package com.happy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.dao.FollowMapper;
import com.happy.entity.FollowEntity;
import com.happy.entity.bo.FollowBo;
import com.happy.service.IFollowService;
import com.happy.util.StateEnum;
import com.happy.util.StateException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service

public class FollowServiceImpl extends ServiceImpl<FollowMapper, FollowEntity> implements IFollowService {

    @Resource
    private FollowMapper followMapper;

    @Override
    public List<FollowBo> queryList ( FollowEntity followEntity ) {
        if ( followEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<FollowBo> followBos = followMapper.queryList ( followEntity.getUserId () );
        return followBos;
    }

    @Override
    public FollowEntity addSingle ( FollowEntity followEntity ) {
        if ( followEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        followEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( followEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return followEntity;
    }

    @Override
    public FollowEntity deleteSingle ( FollowEntity followEntity ) {
        if ( followEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( followEntity.getUserId () == null || followEntity.getFriendId () == null ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        int delete = baseMapper.delete ( new QueryWrapper<FollowEntity> ().eq ( "userId", followEntity.getUserId () ).eq ( "friendId", followEntity.getFriendId () ) );
        if ( delete == 0 ) {
            throw new StateException ( StateEnum.DELETE_EXCE );
        }
        return followEntity;
    }
}
